
//
//  AbbNavigationController.m
//  滑动动画
//
//  Created by 8b410-2 on 16/12/10.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "AbbNavigationController.h"
#define kWindow [UIApplication sharedApplication].keyWindow
@interface AbbNavigationController ()
@property (strong,nonatomic) NSMutableArray *snapshotArray;
@property (strong,nonatomic) UIView *backView;
@property (strong,nonatomic) UIImageView *imageView;
@property (strong,nonatomic) UIPanGestureRecognizer *pan;
@property (assign,nonatomic) CGPoint startPoint;
@property (assign,nonatomic) CGPoint endPoint;
@end

@implementation AbbNavigationController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        //数据初始化
        _snapshotArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:_pan];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //截屏并保存截图
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height), NO, 1);
    [self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [_snapshotArray addObject:snapshot];
    [super pushViewController:viewController animated:animated];
}
- (UIViewController*)popViewControllerAnimated:(BOOL)animated{
    [_snapshotArray removeLastObject];
    return [super popViewControllerAnimated:animated];
}
#pragma mark - privatemethods
- (void)panAction:(UIPanGestureRecognizer *)panGestureRecognizer{
    if (self.viewControllers.count == 1) {
        return;
    }
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始滑动");
        self.startPoint = [panGestureRecognizer locationInView:kWindow];
        if (!_backView) {
            _backView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _backView.backgroundColor = [UIColor blackColor];
        }
        if (!_imageView) {
            _imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _imageView.backgroundColor = [UIColor clearColor];
            _imageView.image = [_snapshotArray lastObject];
        }
        [_backView addSubview:_imageView];
        [self.view.superview insertSubview:_backView belowSubview:self.view];
        
    }else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"结束滑动");
        self.endPoint = [panGestureRecognizer locationInView:kWindow];
        [self judgeWhetherToPop];
    }else{
        CGPoint currentPoint =[panGestureRecognizer locationInView:kWindow];
        CGFloat moveX = currentPoint.x - self.startPoint.x;
        [self moveViewMaskWithX:moveX];
    }
}
- (void)moveViewMaskWithX:(CGFloat)moveX{
    if (moveX >= 0 && moveX <= [UIScreen mainScreen].bounds.size.width) {
        CGRect frame = self.view.frame;
        frame.origin.x = moveX;
        self.view.frame = frame;
    }
}
//判断并执行是否pop
- (void)judgeWhetherToPop{
    if (self.endPoint.x - self.startPoint.x > 50) {
        [UIView animateWithDuration:0.3 animations:^{
            [self moveViewMaskWithX:[UIScreen mainScreen].bounds.size.width];
        } completion:^(BOOL finished) {
            [self popViewControllerAnimated:NO];
            [_backView removeFromSuperview];
            _backView = nil;
            _imageView = nil;
            CGRect frame = self.view.frame;
            frame.origin.x = 0;
            self.view.frame = frame;
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            [self moveViewMaskWithX:0];
        } completion:^(BOOL finished) {
            [_backView removeFromSuperview];
            _backView = nil;
            _imageView = nil;
        }];
    }
}


@end
